#!/usr/bin/python
#coding=utf8
# vim: set fileencoding=utf8 sts=4 sw=4 expandtab :
"""
 Copyright (C) 2008 Xupeng Yun <recordus@gmail.com>
 
 This file is part of videoapi. videoapi is a python lib/client for video sites such as www.tudou.com.

 Videoapi is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 Videoapi is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

 $Id:$
"""

import os
import sys
import unittest
sys.path.insert(0, os.path.abspath('.'))
from tudou import *

class MyTestCase(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_TudouVideo(self):
        url = 'http://www.tudou.com/a.flv'
        title = 'Video title'
        img = 'http://www.tudou.com/img.jpg'
        tv = TudouVideo(url, title, img)
        xml = '<video img="http://www.tudou.com/img.jpg" ' \
            + 'url="http://www.tudou.com/a.flv" title="Video title" />'
        self.assertEqual(tv.toXML(), xml)

    def test_get_flv_id1(self):
        url = 'http://www.tudou.com/playlist/playindex.do?' \
            + 'lid=3451915&iid=8315270'
        self.assertEqual(get_flv_id(url), '8315270')

    def test_get_flv_id2(self):
        url = 'http://www.tudou.com/programs/view/eDb8DyBXpg4/'
        self.assertEqual(get_flv_id(url), '17107668')

    def test_get_flv_info_by_iid(self):
        info = get_flv_info_by_iid('17107668')
        self.assertEqual(type(info), type({}))
        self.assertTrue('17107668.flv' in info['flv_urls'][0])

    def test_get_flv_info_by_url1(self):
        url = 'http://www.tudou.com/playlist/playindex.do?' \
            + 'lid=3451915&iid=8315270'
        info = get_flv_info_by_url(url)
        self.assertEqual(type(info), type({}))
        self.assertTrue('8315270.flv' in info['flv_urls'][0])

    def test_get_flv_info_by_url2(self):
        url = 'http://www.tudou.com/programs/view/eDb8DyBXpg4/'
        info = get_flv_info_by_url(url)
        self.assertEqual(type(info), type({}))
        self.assertTrue('17107668.flv' in info['flv_urls'][0])


class MyTestSuite(unittest.TestSuite):
    def __init__(self):
        ts = []
        for i in dir(MyTestCase):
            if i.startswith('test') and callable(getattr(MyTestCase, i)):
                ts.append(i)
        unittest.TestSuite.__init__(self, map(MyTestCase, tuple(ts)))

def suite():
    return unittest.makeSuite(MyTestCase, 'test')

if __name__ == '__main__':
    #unittest.main(defaultTest = 'suite')
    #runner = unittest.TextTestRunner()
    #runner.run(suite())
    unittest.main()
